package GUI;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.KeyboardFocusManager;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import DataObjects.Bon;
import DataObjects.MetaDataOrder;
import DataObjects.Payment;
import Messages.MessageAddDiscount;
import Messages.MessageAddNewPaymentOfTable;
import Messages.MessageCheckBalancePresentCard;
import Messages.MessageCheckBalancePresentCardReply;
import Messages.MessageDeleteDiscount;
import Messages.MessageSavingReply;

public class VKForDiscount extends JPanel implements ActionListener {
	
	
	private JTextField txt;
	private Color btnColor;
	private Dimension screenSize;
	private int x;
	private int y;
	private MainMenu mainMenu;
	private JTextField txtF;
	
	private PfPointOfSale salePoint;
	private Vector<Bon> bon;
	
	private Float totalPrice;
	private Float balance;
	
	private JButton btn;
	private JButton btnShek;
	
	private MetaDataOrder metaDataOrder;
	
	
	

	public VKForDiscount ()
	{
		super();
		setLayout(new GridLayout(4, 3,10,10));
		initialize();
    }
	
	public VKForDiscount (Color c,
			              final PfPointOfSale salePoint,
			              final Vector<Bon> bon,
			              Float totalPrice,
			               MainMenu mm ,
			               MetaDataOrder metaDataOrder )
	{
		super();
		this.mainMenu=mm;
		this.btnColor=c;
		this.salePoint=salePoint;
		this.bon=bon;
		this.totalPrice=totalPrice;
		this.metaDataOrder=metaDataOrder;
		setLayout(new GridLayout(5, 3,10,10));
        initialize();
    }
	
	
	private void initialize()
	{
		screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		x=screenSize.width;
		y=screenSize.height;
		for(int i = 1; i <= 9; i++) 
        	createButton(Integer.toString(i));
        createPercentButton();
        createNullButton();
        createShekelButton();
        createBackButton();
        createCancelDiscountButton();
        createBSpaceButton();
	}

	private void createPercentButton()
    {
		
        btn = new JButton("%");
        btn.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
                Vector<String> disCount = new Vector<String>();
				
				disCount.addElement(metaDataOrder.getOrderId());    //OrderID
				disCount.addElement(txtF.getText());    //amount
				disCount.addElement(btn.getText());    //type
				MessageAddDiscount msgPayment= new MessageAddDiscount(disCount);
				try {
					mainMenu.ms.client.sendToServer(msgPayment);
					MessageSavingReply result = (MessageSavingReply)mainMenu.ms.client.GetMessage();
					if (result.getResult()== '0')
			    	{
			    		JOptionPane.showMessageDialog(null,"בעיית תקשורת 03");
			    		return;
			    	}
					
						salePoint.calculatePricesForLefrPanel();
						txtF.setText("");
			    					    	
			    	
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				btn.setEnabled(false);
				btnShek.setEnabled(false);
			}
			
		});
        if(btnColor!=null)
        {
        	btn.setBackground(btnColor);
        }
        btn.setFocusable(false);
        btn.setFocusPainted(false);
        btn.setPreferredSize(new Dimension(100, 100));
        btn.setBackground(Color.RED);
        Font font = btn.getFont();
        float size = font.getSize() + 15.0f;
        btn.setFont(font.deriveFont(size));
        add(btn);
        
    }
	
	private void createShekelButton()
    {
		
        btnShek = new JButton("ש''ח");
        btnShek.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
                Vector<String> disCount = new Vector<String>();
				
				disCount.addElement(metaDataOrder.getOrderId());    //OrderID
				disCount.addElement(txtF.getText());    //amount
				disCount.addElement(btnShek.getText());    //type
				MessageAddDiscount msgPayment= new MessageAddDiscount(disCount);
				try {
					mainMenu.ms.client.sendToServer(msgPayment);
					MessageSavingReply result = (MessageSavingReply)mainMenu.ms.client.GetMessage();
					if (result.getResult()== '0')
			    	{
			    		JOptionPane.showMessageDialog(null,"בעיית תקשורת 03");
			    		return;
			    	}
			    					    	
					salePoint.calculatePricesForLefrPanel();
					txtF.setText("");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				btnShek.setEnabled(false);
				btn.setEnabled(false);
			}
			
		});
        if(btnColor!=null)
        {
        	btnShek.setBackground(btnColor);
        }
        btnShek.setFocusable(false);
        btnShek.setFocusPainted(false);
        btnShek.setPreferredSize(new Dimension(100, 100));
        Font font = btnShek.getFont();
        btnShek.setBackground(Color.BLUE);
        float size = font.getSize() + 15.0f;
        btnShek.setFont(font.deriveFont(size));
        add(btnShek);
        
    }
	
	private void createBackButton()
    {
		
        JButton btn = new JButton("בטל");
        btn.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
				 salePoint.remove(salePoint.rightSecondDiscountPanel);
			     salePoint.add(salePoint.getRightPanel(salePoint, bon));
			     salePoint.repaint();
			}
			
		});
        if(btnColor!=null)
        {
        	btn.setBackground(btnColor);
        }
        btn.setFocusable(false);
        btn.setFocusPainted(false);
        btn.setPreferredSize(new Dimension(100, 100));
        btn.setBackground(Color.GREEN);
        Font font = btn.getFont();
        float size = font.getSize() + 15.0f;
        btn.setFont(font.deriveFont(size));
        add(btn);
        
    }
	
	private void createCancelDiscountButton()
    {
		
        JButton btnCanDis = new JButton("בטל הנחה");
        btnCanDis.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
				MessageDeleteDiscount msgDeleteDiscount= new MessageDeleteDiscount(metaDataOrder.getOrderId());
				try {
					mainMenu.ms.client.sendToServer(msgDeleteDiscount);
					MessageSavingReply result = (MessageSavingReply)mainMenu.ms.client.GetMessage();
					if (result.getResult()== '0')
			    	{
			    		JOptionPane.showMessageDialog(null,"בעיית תקשורת 03");
			    		return;
			    	}
			    	
					salePoint.calculatePricesForLefrPanel();
					txtF.setText("");
			    	
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				btn.setEnabled(true);
				btnShek.setEnabled(true);
			}
			
		});
        if(btnColor!=null)
        {
        	btnCanDis.setBackground(btnColor);
        }
        btnCanDis.setFocusable(false);
        btnCanDis.setFocusPainted(false);
        btnCanDis.setPreferredSize(new Dimension(100, 100));
        btnCanDis.setBackground(Color.GREEN);
        Font font = btnCanDis.getFont();
        float size = font.getSize() + 12.0f;
        btnCanDis.setFont(font.deriveFont(size));
        add(btnCanDis);
        
    }
	
    private void createButton(String label)
    {
        JButton btn = new JButton(label);
        if(btnColor!=null)
        {
        	btn.setBackground(btnColor);
        }
        
        btn.setFocusable(false);
        btn.addActionListener(this);
        btn.setFocusPainted(false);
        btn.setPreferredSize(new Dimension(100, 100));
        Font font = btn.getFont();
        float size = font.getSize() + 15.0f;
        btn.setFont(font.deriveFont(size));
        add(btn);
    }
    
    private void createNullButton()
    {
        JButton nullBtn = new JButton("0");
        if(btnColor!=null)
        {
        	nullBtn.setBackground(btnColor);
        }
        nullBtn.setFocusable(false);
        nullBtn.addActionListener(this);
        nullBtn.setFocusPainted(false);
        nullBtn.setPreferredSize(new Dimension(100, 100));
        Font font = nullBtn.getFont();
        float size = font.getSize() + 15.0f;
        nullBtn.setFont(font.deriveFont(size));
        add(nullBtn);
    }
    
    private void createBSpaceButton	()
    {
    	JButton bSpaceBtn=new JButton("BSpace");
    	
    	bSpaceBtn.setFocusable(false);
    	bSpaceBtn.setBackground(Color.GREEN);
    	bSpaceBtn.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
				Component component=KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
		        JTextField txtF=(JTextField)component;
		        int caret=txtF.getCaretPosition();
		        String text=txtF.getText();	
		        
		        txtF.setText(text.substring(0,caret-1) + text.substring(caret));
		        txtF.setCaretPosition(caret-1);
			}
		});
    	
    	bSpaceBtn.setPreferredSize(new Dimension(100, 100));
        Font font = bSpaceBtn.getFont();
        float size = font.getSize()+x/140;
        bSpaceBtn.setFont(font.deriveFont(size));
        add(bSpaceBtn);
    }
    

    @Override
    public void actionPerformed(ActionEvent e)
    {
    	String actionCommand = e.getActionCommand();
        Component component=KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
        txtF=(JTextField)component;
        int caret=txtF.getCaretPosition();
        String text=txtF.getText();
        
                   
        txtF.setText(text.substring(0,caret) +actionCommand+ text.substring(caret));
        txtF.setCaretPosition(caret+txtF.getText().length());
    }
	
    
    public JTextField getTxt() {
		return txt;
	}

	public void setTxt(JTextField txt) {
		this.txt = txt;
	}
	
	public void setBtnColor(Color c)
	{
		this.btnColor=c;
	}
	

}


